//import javax.servlet.ServletException;
//import javax.servlet.http.HttpServlet;
//import javax.servlet.http.HttpServletRequest;
//import javax.servlet.http.HttpServletResponse;
//import java.io.IOException;
//import java.sql.*;
//
//public class login extends HttpServlet {
//    @Override
//    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//        //解决中文乱码问题
//        request.setCharacterEncoding("UTF-8");
//        //获取用户名和密码
//        String username=request.getParameter("username");
//        String password=request.getParameter("password");
//        //JDBC连接数据库
//        Connection conn=null;
//        PreparedStatement ps=null;
//        ResultSet rs=null;
//        boolean loginSuccess = false;
//        try {
//            //注册驱动
//            Class.forName("com.mysql.jdbc.Driver");
//            //获取连接
//            conn= DriverManager.getConnection("jdbc:mysql://localhost/bjpowernode","root","19970913Xun");
//            //获取数据库操作对象
//            String sql = "select id,username,password from t_user01 where username=? and password=?";
//            ps=conn.prepareStatement(sql);
//            ps.setString(1,username);
//            ps.setString(2,password);
//            //执行sql语句
//            rs=ps.executeQuery();
//            //处理查询结果集
//            if (rs.next()){
//                loginSuccess=true;
//            }
//        } catch (ClassNotFoundException e) {
//            e.printStackTrace();
//        } catch (SQLException throwables) {
//            throwables.printStackTrace();
//        }finally {
//            //释放资源
//            if (rs != null) {
//                try {
//                    rs.close();
//                } catch (SQLException throwables) {
//                    throwables.printStackTrace();
//                }
//
//            }
//            if (ps != null) {
//                try {
//                    rs.close();
//                } catch (SQLException throwables) {
//                    throwables.printStackTrace();
//                }
//            }
//            if (conn != null) {
//                try {
//                    rs.close();
//                } catch (SQLException throwables) {
//                    throwables.printStackTrace();
//                }
//            }
//        }
//        //如果登录成功则跳转至成功页面
//        if (loginSuccess){
//            response.sendRedirect(request.getContextPath()+"/success.html");
//        }
//    }
//}
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.*;

public class login extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        //获取表单提交的数据
        String username=request.getParameter("username");
        String password=request.getParameter("password");
        //JDBC连接数据库
        Connection conn=null;
        PreparedStatement ps=null;
        ResultSet rs=null;
        boolean loginSuccess=false;
        //用于打印输出动态用户名需要
        String realname = null;
        try {
            //注册驱动
            Class.forName("com.mysql.jdbc.Driver");
            //获取连接
            conn= DriverManager.getConnection("jdbc:mysql://localhost/bjpowernode","root","19970913Xun");
            //关闭自动提交机制
            conn.setAutoCommit(false);
            //获取数据库操作对象
            String sql = "select username,password,realname from t_user02 where username=? and password=?";
            ps=conn.prepareStatement(sql);
            ps.setString(1,username);
            ps.setString(2,password);
            //执行sql语句
            rs=ps.executeQuery();
            //处理查询结果集
            if (rs.next()){
                loginSuccess=true;
                 realname= rs.getString("realname");
            }
            //提交事务
            conn.commit();
        } catch (ClassNotFoundException e) {
            if (conn!=null){
                try {
                    conn.rollback();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
            e.printStackTrace();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            if (ps != null) {
                try {
                    ps.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
            if (rs != null){
                try {
                    rs.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
        }
        if (loginSuccess){
            //登录成功后判断用户是否点击勾选了十天内免登录
            //获取复选框的value
            String tenDaysNotLogin=request.getParameter("TenDaysNotLogin");
            //判断用户是否勾选了十天内免登录
            if ("OK".equals(tenDaysNotLogin)){
                //勾选之后创建cookie对象
                Cookie cookie1=new Cookie("username",username);
                Cookie cookie2=new Cookie("password",password);
                //设置有效时间
                cookie1.setMaxAge(60*60*24*10);
                cookie2.setMaxAge(60*60*24*10);
                //设置关联路径，即输入该路径就向浏览器发送创建的Cookie
                cookie1.setPath(request.getContextPath());
                cookie2.setPath(request.getContextPath());
                //发送Cookie给浏览器
                response.addCookie(cookie1);
                response.addCookie(cookie2);
            }
            //登录成功则打印成功登录信息
           response.setContentType("text/html;charset=UTF-8");
           PrintWriter out=response.getWriter();
           out.print("<html>");
           out.print("<head>");
           out.print("<title>Success Login!</title>");
           out.print("</head>");
           out.print("<body>");
           out.print("<h2 align='center'>欢迎");
           //动态显示登录成功的用户真实姓名
           out.print(realname );
           out.print("登录成功！！！</h2>");
           out.print("</body>");
           out.print("</html>");
        }else {
            response.sendRedirect(request.getContextPath()+"/Error.html");}
    }
}
